package com.greenlife.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.greenlife.common.enums.BaseResultCodeEnum;
import com.greenlife.common.enums.SessionEnum;
import com.greenlife.common.result.CommonResult;
import com.greenlife.common.result.ListResult;
import com.greenlife.common.result.SingleResult;
import com.greenlife.model.Shop;
import com.greenlife.service.ShopService;

@Controller
@RequestMapping("/shop")
public class ShopController extends BaseController {
	Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private ShopService shopService;

	/** 1.店家注册接口 **/
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult shopRegister(@ModelAttribute(" shop ") Shop shop) {
		Shop values = shopService.insertSelective(shop);
		SingleResult<Shop> result = new SingleResult<Shop>(BaseResultCodeEnum.ERR_CODE_200, values);
		logger.debug("店家注册成功");
		return result;
	}

	/** 2.更新店家信息接口 **/
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult shopUpdate(@ModelAttribute(" shop ") Shop shop) {
		shopService.updateByPrimaryKeySelective(shop);
		CommonResult result = new CommonResult(BaseResultCodeEnum.ERR_CODE_200);
		logger.debug("更新店铺信息成功");
		return result;
	}

	/** 3.查询店铺列表接口 **/
	@RequestMapping(value = "/get", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult shopSelectUser(@ModelAttribute(" shop ") Shop shop, HttpSession session) {
		shop.setUserId((int) session.getAttribute(SessionEnum.USERID));
		List<Shop> values = shopService.selectByUserId(shop);
		ListResult<Shop> result = new ListResult<Shop>(BaseResultCodeEnum.ERR_CODE_200, values);
		logger.debug("查询成功");
		return result;
	}

	/** 4.查询店家详情接口 **/
	@RequestMapping(value = "/select", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult shopSelect(@ModelAttribute(" shop ") Shop shop) {
		Shop values = shopService.selectByPrimaryKey(shop);
		SingleResult<Shop> result = new SingleResult<Shop>(BaseResultCodeEnum.ERR_CODE_200, values);
		logger.debug("查询成功");
		return result;
	}

	/** 5.删除店家接口 **/
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult shopDelete(@ModelAttribute(" shop ") Shop shop) {
		shopService.deleteByPrimaryKey(shop);
		CommonResult result = new CommonResult(BaseResultCodeEnum.ERR_CODE_200);
		logger.debug("删除店家成功");
		return result;
	}

}
